package treeset.demo02;

import java.util.TreeSet;

//使用有参构造创建TreeSet集合,添加学生对象并遍历
public class TreeSetDemo {

    //入口
    public static void main(String[] args) {
        //创建学生对象
        Student student1 = new Student("王子文",23);
        Student student2 = new Student("张子凡",25);
        Student student3 = new Student("李林夕",22);
        Student student4 = new Student("郭东辉",18);
        Student student5 = new Student("切本那",23);

        //创建集合对象
        TreeSet<Student> treeSet = new TreeSet<Student>((o1, o2) -> {
            int i = o1.getAge() - o2.getAge();
            int i1 = i == 0 ? o1.getName().compareTo(o2.getName()): i;
            return i1;
        });

        //添加对象
        treeSet.add(student1);
        treeSet.add(student2);
        treeSet.add(student3);
        treeSet.add(student4);
        treeSet.add(student5);

        //遍历
        for (Student s:treeSet
             ) {
            System.out.println(s);
        }

    }
}
